package com.amphora.web.actions;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.amphora.model.Category;
import com.amphora.model.TreatmentType;
import com.amphora.services.application.BusinessServices;
import com.amphora.services.application.ClientServices;
import com.amphora.services.application.impl.BusinessServicesImpl;
import com.amphora.services.application.impl.ClientServiceImpl;

@WebServlet("/BookingAction")
public class BookingAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String BOOKING_CAN_PROGRESS = "/start_booking.jsp";
	private static String BOOKING_ERROR = "/start_booking.jsp";
	private static String ERROR_ = "/error.jsp";
	private ClientServices cs;
	private BusinessServices treatmentController;
	
	public void init(ServletConfig config) throws ServletException {
		System.out.println("BookingAction init method");
		cs = new ClientServiceImpl();
		treatmentController = new BusinessServicesImpl();
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("BookingAction get method");
		HttpSession session = request.getSession();
		Map parameters = request.getParameterMap();
		String forward = "";	
		
		if(parameters.containsKey("view")){
				if(session.getAttribute("loginID")!=null){
					System.out.println("BookingAction: client logged into application");
					List<TreatmentType> allTreatments = treatmentController.viewAllTreatmentType();
					System.out.println("Amount of treatment types found: " + allTreatments.size());
					
					List<Category> allCategories = treatmentController.viewAllCategories();
					System.out.println("Amount of categories found: " + allCategories.size());
					
					request.setAttribute("categories", allCategories);
					request.setAttribute("treatment", allTreatments);
					
					forward = BOOKING_CAN_PROGRESS;
				}else{
					System.out.println("BookingAction: client not logged into application");
					request.setAttribute("message", "Please login to book a treatment. If this is your first time please register.");
					forward = BOOKING_ERROR;
				}
		}else{
			forward = ERROR_;
		}
		RequestDispatcher view = request.getRequestDispatcher(forward);
		view.forward(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("BookingAction post method");
		HttpSession session = request.getSession();
		Map parameters = request.getParameterMap();
		String forward = "";	
		
		
		RequestDispatcher view = request.getRequestDispatcher(forward);
		view.forward(request,response);
	}

}
